package com.purong.purong.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.purong.purong.Contant;
import com.purong.purong.R;
import com.purong.purong.adapter.CarModelAdapter;
import com.purong.purong.bean.CarBrandBean;
import com.purong.purong.bean.CarModelBean;
import com.purong.purong.bean.CarModelListBean;
import com.purong.purong.bean.TextItemBean;
import com.purong.purong.widget.LoadingView;
import com.purong.purong.widget.RadiusImageView;
import com.yao.sdk.glide.GlideUtil;
import com.yao.sdk.net.ResponseInfo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zengwenjie on 2017/12/19.
 */
public class CarModelActivity extends BaseActivity {
    private CarModelAdapter mAdapter;
    private List<CarModelBean> itemList=new ArrayList<>();
    private RecyclerView mRecyclerView;
    private RadiusImageView img;
    private TextView title;
    private CarBrandBean brandBean;
    private LoadingView loadingView;
    @Override
    public int getRootViewId() {
        return R.layout.activity_car_model;
    }

    @Override
    public void initBundle() {
        super.initBundle();
        brandBean=(CarBrandBean)getIntent().getSerializableExtra("CAR_BRAND");
    }
    private void getModel(){
        loadingView.loading();
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("car_brand_id",brandBean.getCar_brand_id());
        if (!request(Contant.CAR_MODEL,params,true,false)){
            loadingView.fail();
        }
    }
    @Override
    public void initView() {
        showTitle("选择车辆型号");
        showLeft(R.drawable.home_icon_close, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CarModelActivity.this.finish();
            }
        });
        loadingView=(LoadingView)findViewById(R.id.loading_view);
        loadingView.setNoResultToast("暂无相关型号","");
        loadingView.setLoadListener(new LoadingView.OnLoadListener() {
            @Override
            public void onReload() {
                getModel();
            }

            @Override
            public void onRefresh() {

            }
        });
        img=(RadiusImageView)findViewById(R.id.img);
        title=(TextView)findViewById(R.id.title);
        mRecyclerView=(RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mAdapter=new CarModelAdapter(this);
        mAdapter.setData(itemList);
        mAdapter.setOnCarModelListener(new CarModelAdapter.OnCarModelListener() {
            @Override
            public void onModelClick(int position) {
                Intent intent=new Intent();
                intent.putExtra("CAR_MODEL",itemList.get(position));
                setResult(RESULT_OK,intent);
                CarModelActivity.this.finish();
            }
        });
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.CAR_MODEL)){
            if (responseInfo.isRequestOk){
                try {
                    CarModelListBean bean= JSON.parseObject(responseInfo.content, CarModelListBean.class);
                    if (bean!=null&&bean.getCar_model_list()!=null){
                        itemList.clear();
                        itemList.addAll(bean.getCar_model_list());
                        mAdapter.notifyDataSetChanged();
                        if (itemList.size()>0){
                            loadingView.finish();
                        }else {
                            loadingView.noResult();
                        }
                    }
                }catch (Exception e){
                    loadingView.fail();
                }
            }else {
                loadingView.fail();
            }

        }
    }

    @Override
    public void initData() {
        if (brandBean!=null){
            GlideUtil.getInstance().load(this,brandBean.getCar_brand_url(),img,true);
            title.setText(brandBean.getCar_brand_name());
            getModel();
        }

    }
}
